package demo.letcode.top100;

/**
 * 我们把数组中符合下列属性的任意连续子数组称为山脉:
 * b.length >= 3
 * 存在0<i<b.[length-1]使得
 * b[0]<b1<...b[i-1]<b[i]>b[i+1]>b[i+1]...b[b.length-1]
 * 给出一个整数数组a，返回最长'山脉'的长度
 *
 * 输入
 * [2,1,4,7,3,2,5]
 * 输出
 * 5
 * 说明
 * [1,4,7,3,2]，长度为5
 *
 * 输入
 * [2,2,2]
 * 输出0
 *
 * @author taqo
 * @date 2021/6/1
 */
public class _数组中的最长山脉 {
    public static void main(String[] args) {
        int[] a = {2, 1, 4, 7, 3, 2, 5};
        longestMountain(a);
        longestMountain2(a);
    }

    private static void longestMountain2(int[] a) {
        int max = 0,up=0;
    }

    private static void longestMountain(int[] a) {
        int len = a.length;
        // 上升元素 a[i] < a[i+1]
        int[] up = new int[len];
        for (int i = 1; i < len; i++) {
            if (a[i - 1] < a[i]) {
                up[i] = up[i - 1] + 1;
            }
        }

        // 下降元素 a[i-1] > a[i]
        int[] down = new int[len];
        for (int i = len - 1; i > 0; i--) {
            if (a[i - 1] > a[i]) {
                down[i - 1] = down[i] + 1;
            }
        }

        int max = 0;
        for (int i = 0; i < len; i++) {
            if (up[i] == 0 || down[i] == 0) {
                continue;
            }
            max = Math.max(max, up[i] + down[i] + 1);
        }
        System.out.println(max);
    }


}
